#include "sr.h"

sr_type sr;
static uint32_t sr_buf;

static void sr_task(void *arg);

void sr_init(void)
{
    HV_CLK_L;HV_DIN_L;HV_LE_L;
    sr_senddata(0x0);//POL高电平，发送0表示漏极开路输出
    sr.state = 0;
}

void sr_senddata(uint32_t val)
{
    uint32_t tmp = val;
    for(uint8_t i = 0; i < 32; i ++)
    {
        if(tmp & 1)
        {
            HV_DIN_H;
        }
        else
        {
            HV_DIN_L;
        }
        tmp >>= 1;
        
        HV_CLK_H;
        HV_CLK_L;
    }
    HV_LE_H;
    HV_LE_L;
    HV_DIN_L;
}

void sr_display(uint8_t num, bool N2_LD, bool N2_RD, bool N1_LD, bool N1_RD)
{
    sr_buf = 0x0;
    uint8_t n1 = num % 10;
    uint8_t n2 = num / 10 % 10;
    switch(n1)
    {
        case 0:sr_buf |= 1 << 27;break;
        case 1:sr_buf |= 1 << 28;break;
        case 2:sr_buf |= 1 << 30;break;
        case 3:sr_buf |= 1 << 24;break;
        case 4:sr_buf |= 1 << 26;break;
        case 5:sr_buf |= 1 << 23;break;
        case 6:sr_buf |= 1 << 25;break;
        case 7:sr_buf |= 1 << 22;break;
        case 8:sr_buf |= 1 << 29;break;
        case 9:sr_buf |= 1 << 31;break;
        default:break;
    }
    switch(n2)
    {
        case 0:sr_buf |= 1 << 4;break;
        case 1:sr_buf |= 1 << 5;break;
        case 2:sr_buf |= 1 << 7;break;
        case 3:sr_buf |= 1 << 1;break;
        case 4:sr_buf |= 1 << 3;break;
        case 5:sr_buf |= 1 << 10;break;
        case 6:sr_buf |= 1 << 2;break;
        case 7:sr_buf |= 1 << 9;break;
        case 8:sr_buf |= 1 << 6;break;
        case 9:sr_buf |= 1 << 8;break;
        default:break;
    }
    if(N2_LD)
    {
        sr_buf |= 1 << 11;
    }
    if(N2_RD)
    {
        sr_buf |= 1 << 12;
    }
    if(N1_LD)
    {
        sr_buf |= 1 << 20;
    }
    if(N1_RD)
    {
        sr_buf |= 1 << 21;
    }
    
    sr_senddata(sr_buf);
}

